package com.ray.depot.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.ray.api.service.UcenterApiService;
import com.ray.auth.system.table.entity.Button;
import com.ray.auth.system.table.entity.Dict;
import com.ray.depot.service.MaterialLibraryService;
import com.ray.depot.service.MaterialPropertyService;
import com.ray.depot.service.MaterialService;
import com.ray.depot.table.dto.MaterialDto;
import com.ray.depot.table.entity.Material;
import com.ray.depot.table.entity.MaterialLibrary;
import com.ray.depot.table.entity.MaterialProperty;
import com.ray.depot.util.DataGridUtil;
import com.ray.depot.util.DictJsonUtil;
import com.ray.depot.util.DictLoadUtil;
import com.zjhc.flyhong.common.function.operator.LoginUser;
import com.zjhc.flyhong.common.function.operator.PageDto;
import com.zjhc.flyhong.common.web.Result;
import com.zjhc.web.controller.WebController;

@Controller
@RequestMapping("/depot/property")
public class MaterialPropertyController extends WebController {

	@Autowired
	UcenterApiService ucenterApiService;
	@Autowired
	MaterialPropertyService materialPropertyService;
	@Autowired
	MaterialService materialService;
	@Autowired
	MaterialLibraryService  materialLibraryService;
	/**
	 * 新增页面
	 * 
	 * @param menuId
	 * @param modelAndView
	 * @return
	 */
	@RequestMapping(value = "addView")
	@RequiresPermissions("depot:property:add")
	public ModelAndView addView(String menuId, MaterialProperty record, String materialName, HttpServletRequest request, HttpServletResponse respose,
			ModelAndView modelAndView) {
		LoginUser user = getLoginUser();
		// 查询页面拥有的资源
		Result<Map<String, List<Button>>> buttonsResult = ucenterApiService.queryUserButtonsWithMenu(user, menuId);
		if (buttonsResult.codeSuccess()) {
			modelAndView.addObject("buttons", buttonsResult.getData());
		}
		Map<String,List<Dict>> dicts = DictLoadUtil.loadDict(ucenterApiService,"useStatus");
		modelAndView.addObject("dicts", dicts);
		modelAndView.addObject("materialName", materialName);
		modelAndView.addObject("record", record);
		modelAndView.setViewName("depot/material/property/add");
		return modelAndView;
	}

	/**
	 * 新增
	 */
	@RequestMapping(value = "add")
	@RequiresPermissions("depot:property:add")
	@ResponseBody
	public ResponseEntity<Result<Object>> add(MaterialProperty record, HttpServletRequest request,
			HttpServletResponse respose) {
		LoginUser user = getLoginUser();
		Result<Object> result = materialPropertyService.addMaterialProperty(record, user);
		return doResult(result);
	}

	/*
	 * 修改页面
	 */
	@RequestMapping(value = "editView")
	@RequiresPermissions("depot:property:edit")
	@ResponseBody
	public ModelAndView editView(String menuId, String id, HttpServletRequest request, HttpServletResponse respose,
			ModelAndView modelAndView) {
		LoginUser user = getLoginUser();
		// 查询页面拥有的资源
		Result<Map<String, List<Button>>> buttonsResult = ucenterApiService.queryUserButtonsWithMenu(user, menuId);
		if (buttonsResult.codeSuccess()) {
			modelAndView.addObject("buttons", buttonsResult.getData());
		}
		MaterialProperty record = materialPropertyService.queryMaterialProperty(id, user);
		//查询基础信息
		Material material = materialService.queryMaterial(record.getMaterialId(), user);
		
	    Map<String,List<Dict>> dicts = DictLoadUtil.loadDict(ucenterApiService,"useStatus");
		modelAndView.addObject("dicts", dicts);
		modelAndView.addObject("materialName", material.getMaterialName());
		modelAndView.addObject("record", record);
		modelAndView.setViewName("depot/material/property/edit");
		return modelAndView;
	}

	/**
	 * 修改
	 */
	@RequestMapping(value = "edit")
	@RequiresPermissions("depot:property:edit")
	@ResponseBody
	public ResponseEntity<Result<Object>> update(MaterialProperty params, HttpServletRequest request,
			HttpServletResponse respose) {
		LoginUser user = getLoginUser();
		Result<Object> result = materialPropertyService.updateMaterialProperty(params, user);
		return doResult(result);

	}

	/**
	 * 详情页面
	 */
	@RequestMapping(value = "detailView")
	@RequiresPermissions("depot:property:detail")
	@ResponseBody
	public ModelAndView detailView(String menuId, String id, HttpServletRequest request, HttpServletResponse respose,
			ModelAndView modelAndView) {
		LoginUser user = getLoginUser();
		// 查询页面拥有的资源
		Result<Map<String, List<Button>>> buttonsResult = ucenterApiService.queryUserButtonsWithMenu(user, menuId);
		if (buttonsResult.codeSuccess()) {
			modelAndView.addObject("buttons", buttonsResult.getData());
		}
		MaterialProperty record = materialPropertyService.queryMaterialProperty(id, user);
		modelAndView.addObject("record", record);
		modelAndView.setViewName("depot/material/property/detail");
		return modelAndView;

	}

	/**
	 * 删除页面
	 */
	@RequestMapping(value = "delete")
	@RequiresPermissions("depot:property:delete")
	@ResponseBody
	public ResponseEntity<Result<Object>> delete(String id, HttpServletRequest request, HttpServletResponse respose) {
		LoginUser user = getLoginUser();
		Result<Object> result = materialPropertyService.deleteMaterialProperty(id, user);
		return doSuccess(result);

	}

	/**
	 * 查询页面
	 */
	@RequestMapping(value = "query")
	@RequiresPermissions("depot:property:query")
	@ResponseBody
	public ResponseEntity<Map<String, Object>> query(MaterialProperty params, HttpServletRequest request,
			HttpServletResponse respose) {
		LoginUser user = getLoginUser();
		PageDto<MaterialProperty> page = this.createJQPage(request);
		page = materialPropertyService.queryMaterialProperty(params, page, user);
		return doSuccess(DataGridUtil.dataConvert(page));

	}
	
	/**
	 * 库位设置
	 * 
	 * @param menuId
	 * @param modelAndView
	 * @return
	 */
	@RequestMapping(value = "setView")
	@RequiresPermissions("depot:property:set")
	public ModelAndView setView(String menuId, String propertyId, HttpServletRequest request, HttpServletResponse respose,
			ModelAndView modelAndView) {
		LoginUser user = getLoginUser();
		// 查询页面拥有的资源
		Result<Map<String, List<Button>>> buttonsResult = ucenterApiService.queryUserButtonsWithMenu(user, menuId);
		if (buttonsResult.codeSuccess()) {
			modelAndView.addObject("buttons", buttonsResult.getData());
		}
		modelAndView.addObject("materialId", propertyId);
		modelAndView.setViewName("depot/material/property/set");
		return modelAndView;
	}
	
	/**
	 * 选中
	 */
	@RequestMapping(value = "select")
	@RequiresPermissions("depot:property:set")
	@ResponseBody
	public ResponseEntity<Result<Object>> select(MaterialLibrary params , HttpServletRequest request,
			HttpServletResponse respose) {
		LoginUser user = getLoginUser();
		Result<Object> result = materialLibraryService.addMaterialLibrary(params, user);
		return doResult(result);

	}
	
	/**
	 * 取消
	 */
	@RequestMapping(value = "cancel")
	@RequiresPermissions("depot:property:set")
	@ResponseBody
	public ResponseEntity<Result<Object>> cancel(MaterialLibrary params , HttpServletRequest request,
			HttpServletResponse respose) {
		LoginUser user = getLoginUser();
		Result<Object> result = materialLibraryService.deleteMaterialLibrary(params, user);
		return doResult(result);

	}
	
	/**
	 * 查询页面 (不需要权限)
	 */
	@RequestMapping(value = "selectQuery")
	@ResponseBody
	public ResponseEntity<Map<String, Object>> selectQuery(MaterialDto params, HttpServletRequest request,
			HttpServletResponse respose) {
		LoginUser user = getLoginUser();
		PageDto<MaterialDto> page = this.createJQPage(request);
		page = materialPropertyService.querySelectMaterialProperty(params, page, user);
		return doSuccess(DataGridUtil.dataConvert(page));

	}
	
	@RequestMapping(value = "selectView")
	public ModelAndView select(String menuId, String isSerialize, HttpServletRequest request, HttpServletResponse respose,
			ModelAndView modelAndView) {
		LoginUser user = getLoginUser();
		// 查询页面拥有的资源
		Result<Map<String, List<Button>>> buttonsResult = ucenterApiService.queryUserButtonsWithMenu(user, menuId);
		if (buttonsResult.codeSuccess()) {
			modelAndView.addObject("buttons", buttonsResult.getData());
		}
		DictJsonUtil dictJsonUtil = new DictJsonUtil();
		  //获取
		dictJsonUtil.addDict(ucenterApiService,"useStatus","materialType","materialUnit","materialLife","yesOrNo");
		modelAndView.addObject(dictJsonUtil.NAME, dictJsonUtil.getList());
		modelAndView.addObject("isSerialize", isSerialize);
		modelAndView.setViewName("depot/material/property/select");
		return modelAndView;
	}
	
	/**
	 * 查询页面 (不需要权限)
	 */
	@RequestMapping(value = "activiti/selectQuery")
	@ResponseBody
	public ResponseEntity<Map<String, Object>> activitiSelectQuery(MaterialDto params,String tenantId, HttpServletRequest request,
			HttpServletResponse respose) {
		LoginUser user = new LoginUser();
		user.setTenantId(tenantId);
		PageDto<MaterialDto> page = this.createJQPage(request);
		page = materialPropertyService.querySelectMaterialProperty(params, page, user);
		return doSuccess(DataGridUtil.dataConvert(page));

	}
	
	@RequestMapping(value = "activiti/selectView")
	public ModelAndView activitiSelect(String menuId, String isSerialize,String tenantId, HttpServletRequest request, HttpServletResponse respose,
			ModelAndView modelAndView) {
		LoginUser user = new LoginUser();
		user.setTenantId(tenantId);
		// 查询页面拥有的资源
		Result<Map<String, List<Button>>> buttonsResult = ucenterApiService.queryUserButtonsWithMenu(user, menuId);
		if (buttonsResult.codeSuccess()) {
			modelAndView.addObject("buttons", buttonsResult.getData());
		}
		DictJsonUtil dictJsonUtil = new DictJsonUtil();
		  //获取
		dictJsonUtil.addDict(ucenterApiService,"useStatus","materialType","materialUnit","materialLife","yesOrNo");
		modelAndView.addObject(dictJsonUtil.NAME, dictJsonUtil.getList());
		modelAndView.addObject("isSerialize", isSerialize);
		modelAndView.addObject("tenantId", tenantId);
		modelAndView.setViewName("depot/material/property/select");
		return modelAndView;
	}

}
